package com.sfh.cp.mass;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sfh.cp.R;
import com.sfh.cp.base.AbstractBaseActivity;
import com.sfh.cp.mass.pst.IPstMassAddContent;
import com.sfh.cp.util.EmotionKeyboard;
import com.sfh.cp.util.UtilFace;
import com.sfh.cp.widget.WidgetBaseToolbar;
import com.sfh.cp.widget.WidgetFaceLayout;
import com.sfh.cp.widget.WidgetMultiSelectPictureLayout;
import com.sfh.lib.mvp.IPresenter;
import com.sfh.lib.mvp.service.LoaderService;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crm.sfh.server.entity.EMass;
import me.nereo.multi_image_selector.MultiImageSelector;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;

/**
 * 发帖
 */

public class MassAddContentActivity extends AbstractBaseActivity implements IPstMassAddContent.IVAddMassContent {


    public static void startUI(Context context, EMass mass) {

        Intent intent = new Intent(context, MassAddContentActivity.class);
        intent.putExtra("DATA", mass.getMassId());
        context.startActivity(intent);
    }

    public static void startUI(Context context) {

        Intent intent = new Intent(context, MassAddContentActivity.class);
        context.startActivity(intent);
    }


    EmotionKeyboard mEmotionKeyboard;

    @BindView(R.id.etLable)
    EditText etLable;
    @BindView(R.id.gvImage)
    WidgetMultiSelectPictureLayout gvImage;

    @BindView(R.id.etContent)
    EditText etContent;

    @BindView(R.id.ivFace)
    ImageView ivFace;

    @BindView(R.id.tvSize)
    TextView tvSize;

    @BindView(R.id.lyContent)
    LinearLayout lyContent;

    @BindView(R.id.face_layout)
    WidgetFaceLayout faceLayout;


    @BindView(R.id.toolbar)
    WidgetBaseToolbar toolbar;

    @BindView(R.id.tvSelectMass)
    TextView tvSelectMass;
    String massId;
    int mSize = 9;
    ReqAddMassContent reqAddMassContent = new ReqAddMassContent();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.mass_add_content_activity);
        ButterKnife.bind(this);
        init();
    }


    private void init() {

        toolbar.setBackFinish();
        toolbar.setTitle("CP话题");
        toolbar.setRightView("发布").setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                reqAddMassContent.setLable(etLable.getText().toString());
                reqAddMassContent.setTitle(etContent.getText().toString());
                reqAddMassContent.setMassId(massId);
                pstMassAddContent.add(reqAddMassContent);
            }
        });

        gvImage.setAdapter(9, 5, new WidgetMultiSelectPictureLayout.OnItemSelectListener() {

            @Override
            public void onClickSelect(int size) {

                mSize = size;
                hasPermissions();

            }

            @Override
            public void onClickLook(int position, String data) {

            }

            @Override
            public void onClickDel(int position, String data) {

            }
        });
        massId = getIntent().getStringExtra("DATA");
        if (TextUtils.isEmpty(massId)) {
            tvSelectMass.setVisibility(View.VISIBLE);
        } else {
            tvSelectMass.setVisibility(View.GONE);
        }
        mEmotionKeyboard = EmotionKeyboard.with(this)
                .setEmotionLayout(faceLayout)
                .bindToContent(lyContent)
                .bindToEditText(etContent)
                .bindToEmotionButton(ivFace)
                .build();
        faceLayout.addOnItemSelectListener(new WidgetFaceLayout.OnItemSelectListener() {

            @Override
            public void onClickSelect(String name, int res) {

                String content = etContent.getText().toString().trim() + name;
                SpannableString spannableString = UtilFace.getEmotionContent(MassAddContentActivity.this, etContent, content);
                etContent.setText(spannableString);
                etContent.setSelection(spannableString.length() - 1);
            }
        });
        etContent.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int size = 100 - s.length();
                tvSize.setText(size + "/100");
            }
        });
    }

    IPstMassAddContent pstMassAddContent;

    @Override
    public IPresenter getPresenter() {

        if (pstMassAddContent == null) {
            pstMassAddContent = LoaderService.getService(IPstMassAddContent.class);
        }
        return pstMassAddContent;
    }

    @Override
    public void onFileSuccess(List<String> urls) {

        reqAddMassContent.setUrls(urls);
        gvImage.onReplaceData(urls);

    }

    @Override
    public void onAddSuccess() {

        finish();
    }

    @Override
    public void onListMass(final List<EMass> data) {
        //条件选择器
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                massId = data.get(options1).getMassId();
                tvSelectMass.setText(data.get(options1).getName());
            }
        }).build();
        pvOptions.setPicker(data);
        pvOptions.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10101 && resultCode == RESULT_OK) {
            List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            if (path == null || path.isEmpty()) {
                return;
            }
            pstMassAddContent.updateFile(path);
        }
    }

    @Override
    public String[] getPermissions() {
        return new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    }

    @Override
    public void onPermissionsSuccess() {
        selectImage(mSize);
    }


    private void selectImage(int size) {

        MultiImageSelector.create().showCamera(true)
                .count(size).multi().start(MassAddContentActivity.this, 10101);
    }

    @OnClick(R.id.tvSelectMass)
    public void onViewClicked() {
        pstMassAddContent.listMass();
    }
}
